package com.zheng.hr.rpc.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zheng.common.annotation.BaseService;

import com.zheng.hr.dao.model.HrResume;
import com.zheng.hr.dao.mapper.HrResumeMapper;
import com.zheng.hr.dao.model.HrResumeExperience;
import com.zheng.hr.rpc.api.HrResumeExperienceService;
import com.zheng.hr.rpc.api.HrResumeService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.oa.common.constant.enums.OaStatus;
import com.zheng.oa.common.exception.OaException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * hr_resume 简历库 服务实现类
 * </p>
 *
 * @author likun
 * @since 2018-01-08
 */
@Service
@Transactional(rollbackFor = Exception.class)
@BaseService
public class HrResumeServiceImpl extends BaseServiceImpl<HrResumeMapper, HrResume> implements HrResumeService {
    @Autowired
    private HrResumeExperienceService resumeExperienceService;
    @Autowired
    private HrResumeMapper resumeMapper;

    @Override
    public boolean insetResume(HrResume resume, List<HrResumeExperience> resumeExperiences) throws OaException {
        if (insert(resume)) {
            for (HrResumeExperience resumeExperience : resumeExperiences) {
                resumeExperience.setResumeId(resume.getId());
                if (!resumeExperienceService.insert(resumeExperience)) {
                    throw new OaException("添加工作经历失败！");
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean updateResume(HrResume resume, List<HrResumeExperience> resumeExperiences) throws OaException {
        if (updateById(resume)) {
            EntityWrapper<HrResumeExperience> resumeExperienceEntityWrapper = new EntityWrapper<>();
            resumeExperienceEntityWrapper.where("resume_id=" + resume.getId());
            if (resumeExperienceService.delete(resumeExperienceEntityWrapper)) {
                for (HrResumeExperience resumeExperience : resumeExperiences) {
                    resumeExperience.setResumeId(resume.getId());
                    if (!resumeExperienceService.insert(resumeExperience)) {
                        throw new OaException("新增工作经历失败！");
                    }
                }
            } else {
                return false;
            }
            return true;
        }
        return false;
    }

    public List<Map<String, Object>> selectResumePage(HrResume resume) {
        resume.setStatus(OaStatus.NORMAL.getKey());
        return resumeMapper.selectResumePage(resume);
    }

    public Integer selectResumePageCount(HrResume resume) {
        resume.setStatus(OaStatus.NORMAL.getKey());
        return resumeMapper.selectResumePageCount(resume);
    }
}
